using System;
using UnityEngine;

public class GUIAnchor2 : MonoBehaviour
{
	private void Awake()
	{
		this.position = base.transform.position;
		this.localScale = base.transform.localScale;
		this.RecalcLayout();
	}

	public static bool IsPortrate()
	{
		return Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown || (Screen.orientation != ScreenOrientation.LandscapeLeft && Screen.orientation != ScreenOrientation.LandscapeLeft && Screen.orientation != ScreenOrientation.LandscapeRight && (Screen.orientation != ScreenOrientation.AutoRotation || (!Screen.autorotateToLandscapeLeft && !Screen.autorotateToLandscapeRight)));
	}

	private void RecalcLayout()
	{
		if (null == this.viewCamera)
		{
			if ((this.anchor.left && this.anchor.right) || (this.anchor.top && this.anchor.bottom))
			{
				UnityEngine.Debug.LogWarning("Invalid Camera !!!");
			}
			this.viewCamera = Camera.main;
		}
		if (Application.isEditor)
		{
			this._aspect = ((this.viewCamera.aspect <= 1f) ? 0.6666667f : 1.5f);
		}
		else
		{
			this._aspect = (GUIAnchor2.IsPortrate() ? 0.6666667f : 1.5f);
		}
		float num = this.viewCamera.aspect;
		if ((this._aspect > 1f && num < 1f) || (this._aspect < 1f && num > 1f))
		{
			num = 1f / num;
		}
		if (num == this._aspect)
		{
			return;
		}
		if (num <= this.range.min || num >= this.range.max)
		{
			return;
		}
		this.RecalcLayoutNormal(num);
		this.RecalcLayoutOrthgraphic(num);
	}

	private void RecalcLayoutNormal(float cameraAspect)
	{
		Vector3 vector = this.position;
		Vector3 vector2 = this.localScale;
		if (this.anchor.left && this.anchor.right)
		{
			vector.x = (vector.x - this.viewCamera.transform.position.x) * cameraAspect / this._aspect * this.rate + this.viewCamera.transform.position.x;
			base.transform.position = vector;
		}
		else if (this.anchor.left)
		{
			vector.x += this.viewCamera.orthographicSize * (this._aspect - cameraAspect) * this.rate;
			base.transform.position = vector;
		}
		else if (this.anchor.right)
		{
			vector.x -= this.viewCamera.orthographicSize * (this._aspect - cameraAspect) * this.rate;
			base.transform.position = vector;
		}
		if (this.scale == GUIAnchor2.eScaleType.ALL)
		{
			base.gameObject.transform.localScale *= cameraAspect * this.rate / this._aspect;
		}
		if (this.scale == GUIAnchor2.eScaleType.HORIZON)
		{
			base.transform.localScale = new Vector3(cameraAspect * this.rate / this._aspect * vector2.x, vector2.y, vector2.z);
		}
	}

	private void RecalcLayoutOrthgraphic(float cameraAspect)
	{
		if (cameraAspect >= this._aspect)
		{
			return;
		}
		Vector3 vector = this.position;
		Vector3 vector2 = this.localScale;
		if (this.anchor.top && this.anchor.bottom)
		{
			vector.y = (vector.y - this.viewCamera.transform.position.y) * this._aspect / cameraAspect * this.rate + this.viewCamera.transform.position.y;
			base.transform.position = vector;
		}
		else if (this.anchor.top)
		{
			vector.y += (this._aspect / cameraAspect - 1f) * this.viewCamera.orthographicSize * this.rate;
			base.transform.position = vector;
		}
		else if (this.anchor.bottom)
		{
			vector.y -= (this._aspect / cameraAspect - 1f) * this.viewCamera.orthographicSize * this.rate;
			base.transform.position = vector;
		}
		if (this.scale == GUIAnchor2.eScaleType.ORTH_ALL)
		{
			base.gameObject.transform.localScale *= this._aspect * this.rate / cameraAspect;
		}
		if (this.scale == GUIAnchor2.eScaleType.ORTH_HORIZON)
		{
			base.gameObject.transform.localScale = new Vector3(vector2.x, this._aspect * this.rate / cameraAspect * vector2.y, vector2.z);
		}
	}

	public Camera viewCamera;

	public GUIAnchor2.Range range = new GUIAnchor2.Range();

	public GUIAnchor2.Anchor anchor = new GUIAnchor2.Anchor();

	public GUIAnchor2.eScaleType scale;

	public float rate = 1f;

	private Vector3 position;

	private Vector3 localScale;

	private float _aspect = 1.5f;

	public enum eScaleType
	{
		NONE,
		ALL,
		HORIZON,
		ORTH_ALL,
		ORTH_HORIZON
	}

	[Serializable]
	public class Anchor
	{
		public bool left;

		public bool right;

		public bool top;

		public bool bottom;
	}

	[Serializable]
	public class Range
	{
		public string ToString()
		{
			return string.Format("min({0}), max({1})", this.min, this.max);
		}

		public float min;

		public float max = 2f;
	}
}
